package data;

import java.util.Map.Entry;
import java.util.Vector;

public class TablaSimbolos 
{
	private static TablaSimbolos _instancia = new TablaSimbolos();
	
	private Vector<Entry<String, String>> _variables;
	private Vector<Entry<String, Entry<Vector<Object>,Vector<Object>>>> _metodos;
	
	/**
	 * Constructor
	 */
	private TablaSimbolos()
	{
		_variables = new Vector<Entry<String,String>>();
		_metodos = new Vector<Entry<String,Entry<Vector<Object>,Vector<Object>>>>();
	}
	
	/**
	 * getInstabcia
	 */
	public static TablaSimbolos getInstancia()
	{
		return _instancia;
	}
	
	/**
	 * Insertar variable
	 */
	public String insertarVariable(String nom, String tipo)
	{
		String anterior = existeVariable(nom);
		if ( anterior != null )
			return anterior;
		
		_variables.add(new Par<String, String>(nom, tipo));
		return null;
	}
	
	/**
	 * Insertar metodo
	 */
	public Entry<Vector<Object>, Vector<Object>> insertarMetodo(String nom, Vector<Object> tipo, Vector<Object> valref)
	{
		Entry<Vector<Object>, Vector<Object>> anterior = existeMetodo(nom);

		if ( anterior != null )
			return anterior;
		
		Par<Vector<Object>, Vector<Object>> aux = new Par<Vector<Object>, Vector<Object>>(tipo, valref);
		_metodos.add(new Par<String, Entry<Vector<Object>,Vector<Object>>>(nom, aux));
		return null;
	}
	
	/**
	 * Obtener tipo variable
	 */
	public String getTipoVariable(String nom)
	{
		return existeVariable(nom);
	}
	
	/**
	 * Obtener tipos metodo
	 */
	public Vector<Object> getTiposVarsMetodo(String nom)
	{
		Entry<Vector<Object>, Vector<Object>> aux = existeMetodo(nom);
		if ( aux != null )
			return aux.getKey();
		else
			return null;
	}
	
	/**
	 * Obtener valref del metodo
	 */
	public Vector<Object> getValRefVarsMetodo(String nom)
	{
		Entry<Vector<Object>, Vector<Object>> aux = existeMetodo(nom);
		if ( aux != null )
			return aux.getValue();
		else
			return null;
	}
	
	/**
	 * Existe variable
	 */
	private String existeVariable(String nom)
	{
		for ( Entry<String, String> par : _variables )
			if ( par.getKey().equals(nom) )
				return par.getValue();
		
		return null;
	}
	
	/**
	 * Existe metodo
	 */
	private Entry<Vector<Object>, Vector<Object>> existeMetodo(String nom)
	{
		for ( Entry<String, Entry<Vector<Object>, Vector<Object>>> par : _metodos )
			if ( par.getKey().equals(nom) )
				return par.getValue();
		
		return null;
	}
	
	/**
	 * Obtener variables
	 */
	public Vector<Entry<String, String>> getVariables()
	{
		return _variables;
	}
	
	/**
	 * Obtener metodos
	 */
	public Vector<Entry<String, Entry<Vector<Object>, Vector<Object>>>> getMetodos()
	{
		return _metodos;
	}
}
